package server;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.math.BigInteger;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Servlet implementation class StreetServlet
 */
public class StreetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StreetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String lat=request.getParameterValues("lat")[0];
		String lng=request.getParameterValues("lng")[0];
		PrintWriter out=response.getWriter();
		URL url = new URL (" http://ws.geonames.org/findNearestIntersectionOSM?lat="+lat+"&lng="+lng);
		 InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");  
		 BufferedReader in = new BufferedReader(isr);  
		 String text="";
		 String inputLine;  
		  while ((inputLine = in.readLine()) != null) {  
		        System.out.println(inputLine);  
		        text+=inputLine + "\n";  
		     }  
		  String startTag = "<name>";
		  String endTag = "</name>";
		  int start = text.indexOf(startTag)+startTag.length();
		  int end = text.indexOf(endTag);
		  String result = text.substring(start, end);
		 // JOptionPane.showMessageDialog(null, result);  
		  out.write(result);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	
	
	
}
